package com.atwl.school_system.controller;

import com.atwl.school_system.bean.OrderInfo;
import com.atwl.school_system.bean.vo.OrderInfoVo;
import com.atwl.school_system.mapping.OrderDao;
import com.atwl.school_system.service.OrderService;
import com.atwl.school_system.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDao orderDao;

    /**
     * 获取订单
     */
    @GetMapping("/order/get")
    public R getOrder(@RequestParam(value = "uuid", required = false) String uuid,
                      @RequestParam(value = "username", required = false) String username,
                      @RequestParam(value = "phone", required = false) String phone,
                      @RequestParam(value = "status") Integer status) {
        List<OrderInfoVo> orderInfos = orderService.getOrder(uuid, username, phone, status);
        return R.ok().setData(orderInfos);
    }

    /**
     * 获取用户的order
     *
     * @return
     */
    @GetMapping("/order/getByUserId")
    public R getOrderByUserId(@RequestParam("userId") String userId, @RequestParam(value = "status", required = false) Integer status) {
        List<OrderInfoVo> orderInfoVos = orderService.getOrderByUserId(userId, status);
        return R.ok().setData(orderInfoVos);
    }

    /**
     * 取消订单，逻辑删除
     */
    @DeleteMapping("/order/delete")
    public R deleteOrder(@RequestParam("uuid") String uuid) {
        boolean flag = orderDao.deleteOrder(uuid);
        if (flag) {
            return R.ok();
        } else {
            return R.error("无法删除订单，请联系管理员");
        }
    }

    @PostMapping("/order/add")
    public R createOrder(@RequestBody OrderInfo orderInfo) {
        orderService.createOrderB(orderInfo);
        return R.ok();
    }

    /**
     * 修改订单
     */
    @RequestMapping("/order/update")
    public R updateOrder(@RequestBody OrderInfo orderInfo) {
        boolean flag = orderService.updateOrder(orderInfo);
        if (flag) {
            return R.ok("修改订单成功");
        } else {
            return R.error("修改订单失败，请联系管理员");
        }
    }
}
